package com.superman.socket;

import com.superman.util.SocketFactory;

import java.io.*;
import java.net.Socket;

public class TCPClient {
    /**
     * 获取服务端响应
     * @param request 客户端请求转化成的字符串
     * @return 服务端响应处理后的字符串
     * @throws IOException
     */
    public static String getResponse(String request) throws IOException {
        Socket socket = SocketFactory.getSocket();

        //将传入的字符串输出流写入到socket输出流
        PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
        printWriter.println(request);

        //接收服务端返回的输出流,包装成字符输入流
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        //将返回的输入流转换成字符串并返回
        StringBuffer response = new StringBuffer();
        response.append(bufferedReader.readLine());

        printWriter.close();
        bufferedReader.close();
        socket.close();

        //将服务端返回的流转换成字符串返回
        return response.toString();

    }

}
